package com.zmn.oms.business.impl.work.masterwork;

import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.oms.business.interfaces.work.masterwork.MasterWorkContactBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.common.dio.normal.master.WorkContactInfoDIO;
import com.zmn.oms.common.dro.normal.master.WorkContactInfoDRO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.changerecord.OrderChangeRecord;
import com.zmn.oms.model.entity.detail.OrderDetail;
import com.zmn.oms.model.entity.master.OrderMaster;
import com.zmn.oms.model.entity.masterworking.MasterWorking;
import com.zmn.oms.model.entity.work.OrderWork;
import com.zmn.oms.services.interfaces.changerecord.OrderChangeRecordService;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.master.OrderMasterService;
import com.zmn.oms.services.interfaces.masterworking.MasterWorkingService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.xno.dubbo.interfaces.XnoRemoteService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/** 
 * 类描述：工程师联系相关业务处理
 * 
 * @author  liuying
 * @date   2022/01/13 13:03
 */
@Slf4j
@Primary
@Service
public class MasterWorkContactBServiceImpl implements MasterWorkContactBService {

    @Autowired
    private OrderWorkService orderWorkService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private MasterWorkingService masterWorkingService;
    @Autowired
    private OrderMasterService orderMasterService;
    @Autowired
    private OrderChangeRecordService orderChangeRecordService;

    @Reference(version = com.zmn.xno.common.constant.SystemConstants.DUBBO_CONSTS_INTERFACE_VERSION, check = false)
    private XnoRemoteService xnoRemoteService;


    /**
     * 工程师工单联系信息详情
     * @param workContactInfoDIO
     */
    @Override
    public WorkContactInfoDRO getWorkContactInfo(WorkContactInfoDIO workContactInfoDIO) throws OmsBaseException {
        OrderWork orderWork = orderWorkService.findOrderWorkByKey(workContactInfoDIO.getOrderId(), workContactInfoDIO.getWorkId());
        if (orderWork == null) {
            throw new OmsBaseException("无效的工单");
        }
        if (NumberUtil.isNullOrZero(orderWork.getMasterId())) {
            throw new OmsBaseException("无效的工单");
        }

        OrderDetail orderDetail = orderDetailService.findOrderDetailByKey(workContactInfoDIO.getOrderId());

        MasterWorking masterWorking = masterWorkingService.findMasterWorkingByWorkId(workContactInfoDIO.getOrderId());

        WorkContactInfoDRO workContactInfoDRO = new WorkContactInfoDRO();
        workContactInfoDRO.setOrderId(orderWork.getOrderId());
        workContactInfoDRO.setWorkId(orderWork.getWorkId());
        workContactInfoDRO.setMasterId(orderWork.getMasterId());
        workContactInfoDRO.setDutyTime(orderWork.getDutyTime());
        workContactInfoDRO.setRecommenderId(orderWork.getRecommenderId());
        workContactInfoDRO.setRecommenderType(orderWork.getRecommenderType());

        if (Objects.nonNull(masterWorking)) {
            workContactInfoDRO.setContactTime(masterWorking.getContactTime());
        } else {
            workContactInfoDRO.setContactTime(orderWork.getContactTime());
        }

        // 查询工程师手机号
        List<OrderMaster> orderMasterList = orderMasterService.listMasterByWorkId(workContactInfoDIO.getOrderId(), workContactInfoDIO.getWorkId(), OrderConsts.ORDER_MASTER_TYPE_MAIN);
        workContactInfoDRO.setMasterMobile(orderMasterList.get(0).getMasterPhone());

        // 根据绑定状态处理
        boolean xnoBindStatus = this.getXnoBindStatus(orderDetail.getBindTelephone(), orderDetail.getBindStatus());
        workContactInfoDRO.setBindStatus(xnoBindStatus ? OrderConsts.BIND_STATUS_BIND : GlobalConsts.NONE);
        if (xnoBindStatus) {
            workContactInfoDRO.setTelephone(orderDetail.getBindTelephone());
            workContactInfoDRO.setTelephone2(orderDetail.getBindTelephone2());
            workContactInfoDRO.setTelephone3(orderDetail.getBindTelephone3());
        } else {
            workContactInfoDRO.setTelephone(orderDetail.getTelephone());
            workContactInfoDRO.setTelephone2(orderDetail.getTelephone2());
            workContactInfoDRO.setTelephone3(orderDetail.getTelephone3());
        }

        OrderChangeRecord changeRecord = orderChangeRecordService.getChangeRecordByOrderId(orderWork.getOrderId());
        if (Objects.isNull(changeRecord)) {
            workContactInfoDRO.setAppDutyTimeModifyCount(changeRecord.getAppDutyTimeModifyCount());
        }

        return workContactInfoDRO;
    }

    /**
     * 获取小号的绑定状态
     */
    private boolean getXnoBindStatus(String xno, Integer status) {
        if (!Objects.equals(status, OrderConsts.BIND_STATUS_BIND)) {
            return false;
        }
        if (StringUtils.isBlank(xno)) {
            return false;
        }

        try {
            // 查询小号是否显示真实号码
            ResponseDTO<Boolean> isShowXno = xnoRemoteService.queryExposeByXno(xno);
            log.info("com.zmn.oms.business.impl.work.masterwork.MasterWorkBServiceImpl.getXnoBindStatus 查询小号是否显示真实号码出参{}", isShowXno);
            if (isShowXno.isSuccess() && isShowXno.getData() != null) {
                return !isShowXno.getData();
            }
            return false;
        } catch (Exception e) {
            log.info("com.zmn.oms.business.impl.work.masterwork.MasterWorkBServiceImpl.getXnoBindStatus error {}", e);
            e.printStackTrace();
        }
        return false;
    }

}
